/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.kramerius.commons;

import org.apache.commons.cli.*;

/**
 *
 * @author Martin Řehánek (rehan@mzk.cz)
 */
public class CommandLineInterface {

    private final CommandLine cml;

    public CommandLineInterface(String[] args, CommandLineOptions definedOptions) throws ParseException {
        cml = initCommandLine(args, definedOptions);
    }

    private CommandLine initCommandLine(String[] args, CommandLineOptions definedOptions) throws ParseException, org.apache.commons.cli.ParseException {
        Options options = new Options();
        for (Option option : definedOptions.getAllOptions()) {
            options.addOption(option);
        }
        CommandLineParser parser = new PosixParser();
        return parser.parse(options, args);
    }

    public String getStringValueOrNull(Option option) {
        String opt = option.getOpt();
        if (cml.hasOption(opt)) {
            return cml.getOptionValue(opt);
        } else {
            return null;
        }
    }

    public boolean getBooleanValue(Option option) {
        String opt = option.getOpt();
        if (cml.hasOption(opt)) {
            return true;
        } else {
            return false;
        }
    }
}
